(function (vc, vm) {
    var _fileUrl = '/callComponent/download/getFile/fileByObjId';
    vc.extends({
        data: {
            editBlacklistInfo: {
                blacklistId: '',
                blacklistName: '',
                startTime: '',
                endTime: '',
                remark: '',
                photo: '',
            }
        },
        _initMethod: function () {

        },
        _initEvent: function () {
            vc.on('editBlacklist', 'openEditBlacklistModal', function (_params) {
                vc.component.refreshEditBlacklistInfo();
                $('#editBlacklistModel').modal('show');
                vc.copyObject(_params, vc.component.editBlacklistInfo);
                //根据memberId 查询 照片信息
                vc.component.editBlacklistInfo.photo = _fileUrl + "?objId=" +
                    vc.component.editBlacklistInfo.blacklistId + "&communityId=" + vc.getCurrentCommunity().communityId + "&fileTypeCd=13000&time=" + new Date();
                vc.component.editBlacklistInfo.communityId = vc.getCurrentCommunity().communityId;
            });
        },
        methods: {
            editBlacklistValidate: function () {
                return vc.validate.validate({
                    editBlacklistInfo: vc.component.editBlacklistInfo
                }, {
                    'editBlacklistInfo.blacklistName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "用户名字不能为空"
                        },
                        {
                            limit: "max",
                            param: "1,100",
                            errInfo: "位置名称不能超过100位"
                        },
                    ],
                    'editBlacklistInfo.startTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "开始时间不能为空"
                        },
                        {
                            limit: "dateTime",
                            param: "",
                            errInfo: "不是有效的时间格式"
                        },
                    ],
                    'editBlacklistInfo.endTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "结束时间不能为空"
                        },
                        {
                            limit: "dateTime",
                            param: "",
                            errInfo: "不是有效的时间格式"
                        },
                    ],
                    'editBlacklistInfo.remark': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "备注不能为空"
                        },
                        {
                            limit: "max",
                            param: "1,100",
                            errInfo: "备注不能超过100位"
                        },
                    ],
                    'editBlacklistInfo.blacklistId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "黑名单用户ID不能为空"
                        }]

                });
            },
            editBlacklist: function () {
                if (!vc.component.editBlacklistValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                vc.component.editBlacklistInfo.communityId = vc.getCurrentCommunity().communityId;

                //编辑时 ownerPhoto 中内容不是照片内容，则清空
                if (vc.component.editBlacklistInfo.photo.indexOf(_fileUrl) != -1) {
                    vc.component.editBlacklistInfo.photo = "";
                }
                vc.http.apiPost(
                    'blacklist.updateBlacklist',
                    JSON.stringify(vc.component.editBlacklistInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status == 200) {
                            //关闭model
                            $('#editBlacklistModel').modal('hide');
                            vc.emit('blacklistManage', 'listBlacklist', {});
                            return;
                        }
                        vc.message(json);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.message(errInfo);
                    });
            },
            refreshEditBlacklistInfo: function () {
                vc.component.editBlacklistInfo = {
                    blacklistId: '',
                    blacklistName: '',
                    startTime: '',
                    endTime: '',
                    remark: '',
                    photo: '',
                }
            },
            _uploadPhoto: function (event) {
                $("#uploadPhoto").trigger("click")
            },
            _choosePhoto: function (event) {
                var photoFiles = event.target.files;
                if (photoFiles && photoFiles.length > 0) {
                    // 获取目前上传的文件
                    var file = photoFiles[0];// 文件大小校验的动作
                    if (file.size > 1024 * 1024 * 2) {
                        vc.toast("图片大小不能超过 2MB!")
                        return false;
                    }
                    var reader = new FileReader(); //新建FileReader对象
                    reader.readAsDataURL(file); //读取为base64
                    reader.onload = function (e) {
                        vc.component.editBlacklistInfo.photo = reader.result;
                    }
                }
            },
        }
    });

})(window.vc, window.vc.component);
